//
//  VisitorView.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/11/9.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit

class VisitorView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(visitorHomeImageView)
        addSubview(messageLable)
        
        visitorHomeImageView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        messageLable.xmg_AlignInner(type: XMG_AlignType.BottomCenter, referView: visitorHomeImageView, size: nil, offset: CGPoint(x: 0, y: 60))
        //哪个控件的什么属性 等于另外一个控件的 什么属性 乘以多少 加上多少
        let messageCons = NSLayoutConstraint.init(item: messageLable, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1.0, constant: 250)
        addConstraint(messageCons)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("")
    }
    
    /// 懒加载Home页未登录页面
    private lazy var visitorHomeImageView:UIImageView = {
        let iv = UIImageView(image: UIImage(named: "Visitor_home_back"))
        return iv
    }()
    
    private lazy var messageLable :UILabel = {
        let label =  UILabel()
        label.text = "Go log in. Check out your day's activities here"
        label.numberOfLines = 0
        label.textColor = UIColor.darkGray
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    
    /**
     设置未登录界面
     :param: isHome    是否是首页
     :param: imageName 需要展示的图标名称
     :param: message   需要展示的文本内容
     */
    public func setupVisitorInfo(animationType:Int, imageName:String, message:String){
        // 修改中间图标
        visitorHomeImageView.image = UIImage(named: imageName)
        // 修改文本
        messageLable.text = message
        // 判断是否需要执行动画
        if animationType == 1{
            rotationAnimation()
        }else if animationType==2{
            rollAnimation()
        }else if animationType==3{
            moveAnimation()
        }else if animationType==4{
            coolAnimation()
        }else{
            visitorHomeImageView.layer.removeAllAnimations()
        }
    }
    
    private func rotationAnimation(){
        // 1.创建动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 2.设置动画属性
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        // 该属性默认为YES, 代表动画只要执行完毕就移除
        anim.isRemovedOnCompletion = false
        // 3.将动画添加到图层上
        visitorHomeImageView.layer.add(anim, forKey: nil)
    }
    private func rollAnimation(){
        // 1.创建动画
        let anim = CABasicAnimation(keyPath: "transform.rotation.y")
        // 2.设置动画属性
        anim.toValue = 2 * M_PI
        anim.duration = 10
        anim.repeatCount = MAXFLOAT
        // 该属性默认为YES, 代表动画只要执行完毕就移除
        anim.isRemovedOnCompletion = false
        // 3.将动画添加到图层上
        visitorHomeImageView.layer.add(anim, forKey: nil)
    }
    
    private func moveAnimation(){
        let scaleAnimate = CABasicAnimation(keyPath: "transform.scale")
        scaleAnimate.fromValue = 1.0
        scaleAnimate.toValue = 0.8
        scaleAnimate.autoreverses = true
        scaleAnimate.isRemovedOnCompletion = false
        scaleAnimate.repeatCount = MAXFLOAT
        scaleAnimate.duration = 2.0
        visitorHomeImageView.layer.add(scaleAnimate, forKey: nil)
    }
    
    private func coolAnimation(){
        // 抖动
        let anim = CAKeyframeAnimation()
        anim.keyPath = "transform.rotation"
        // (-M_PI_4 /90.0 * 5)表示-5度 。
        
        let value1 = NSValue(cgPoint: CGPoint(x:CGFloat(-M_PI_4 / 90.0 * 5.0), y:0.0))
        let value2 = NSValue(cgPoint: CGPoint(x:CGFloat(M_PI_4 / 90.0 * 5.0), y:0.0))
        let value3 = NSValue(cgPoint: CGPoint(x:CGFloat(-M_PI_4 / 90.0 * 5.0), y:0.0))
        anim.values = [value1, value2, value3];
        anim.isRemovedOnCompletion = false
        anim.fillMode = CAMediaTimingFillMode.forwards
        anim.duration = 0.2
        anim.repeatCount = MAXFLOAT
        //
        visitorHomeImageView.layer.add(anim, forKey: nil)
    }
}

